﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.IO;
using SUT = Admx_Generator;

namespace Tests_Admx_Generator
{
    [TestClass]
    public class Test_Scenario
    {
        private const string _projectFolder = @"C:\Users\Courtel.MCSA\Documents\Visual Studio 2013\Projects\Admx Generator\Tests_Admx Generator\Test Files\Scenarios\New Project";
        private const string _projectName = "ScenarioTest";
        private const string _admxName = "PstBackup2013";
        private SUT.Namespace _namespace;
        private SUT.culture _fallbackCulture;

        [TestInitialize]
        public void BeforEachTest()
        {
            _namespace = new SUT.Namespace("EasyCompany.Policies.PstBackup2013", "PstBackup");
            _fallbackCulture = new SUT.culture(new System.Globalization.CultureInfo("FR"));
        }

        [TestCleanup]
        public void AfterEachTest()
        {
            DeleteProjectFolder();
        }

        [TestMethod]
        public void CreateAndSaveAnEmptyProject()
        {
            // Arrange
            SUT.AdmxProject project = new SUT.AdmxProject(_projectFolder, _projectName);

            // Act
            project.Save();

            // Assert
            Assert.IsTrue(ProjectExists());
        }

        [TestMethod]
        public void AddAnAdmxToAnEmptyProject()
        {
            // Arrange
            SUT.AdmxProject project = new SUT.AdmxProject(_projectFolder, _projectName);
            SUT.AdmxFile admx = new SUT.AdmxFile(_admxName, _namespace, _fallbackCulture);

            // Act
            project.SetAdmx(admx);
            project.Save();
            SUT.AdmxFile savedAdmx = new SUT.AdmxFile(Path.Combine(_projectFolder, _projectName, _admxName + ".Admx"));

            // Assert
            Assert.IsTrue(ProjectExists());
            Assert.IsTrue(AdmxExists(_admxName));
        }

        [TestMethod]
        public void ReadWindowsUpdateAdmx()
        {
            // Arrange
            SUT.AdmxFile admx = new SUT.AdmxFile(@"C:\Users\Courtel.MCSA\Documents\Visual Studio 2013\Projects\Admx Generator\Tests_Admx Generator\Test Files\Scenarios\WindowsUpdate.admx");

            // Assert


        }

        /// <summary>
        /// Check if _projectFolder exists and if _projectName.AdmxProj exists
        /// </summary>
        /// <returns>True if folder and file exists</returns>
        private static bool ProjectExists()
        {
            DirectoryInfo projectFolder = new DirectoryInfo(Path.Combine(_projectFolder, _projectName));
            FileInfo projectFile = new FileInfo(Path.Combine(projectFolder.FullName, _projectName + ".AdmxProj"));

            return projectFolder.Exists && projectFile.Exists;
        }

        /// <summary>
        /// Check if the admx file exists
        /// </summary>
        /// <param name="admxName">Name of teh admx file.</param>
        /// <returns>True if the file exists</returns>
        private static bool AdmxExists(string admxName)
        {
            DirectoryInfo projectFolder = new DirectoryInfo(Path.Combine(_projectFolder, _projectName));
            FileInfo projectFile = new FileInfo(Path.Combine(projectFolder.FullName, admxName + ".Admx"));

            return projectFolder.Exists && projectFile.Exists;
        }

        /// <summary>
        /// Delete the project folder
        /// </summary>
        private static void DeleteProjectFolder()
        {
            DirectoryInfo projectFolder = new DirectoryInfo(Path.Combine(_projectFolder, _projectName));
            if (projectFolder.Exists)
                projectFolder.Delete(true);
        }
    }
}
